<template>
  <div class="cart">
    <div class="cart-box">
      
      <div class="title">
        <div class="menu" @click="back">
          <i class="iconfont icon-link"></i>
          K.B商店
        </div>
        <i class="iconfont gengduo icon-more"></i>
        <p class="type">购物车</p>
      </div>

      <hr>

      <div class="order">
        <div class="item-title">
          <div class="col-7">商品</div>
          <div class="col-2">数量</div>
          <div class="col-2">价格</div>
          <div class="col-1">操作</div>
        </div>
        <div class="item-list">
          <div class="item-container" v-for="(item,index) of cart" :key="item._id">
            <div class="col-1">
              <div @click="handleSelect(index)" class="checkbox">
                <i v-show="item.isSelected" class="iconfont selected icon-selected"></i>
              </div>
            </div>
            <div class="col-6">
              <div class="img-cover">
                <img :src="item.img" alt="">
              </div>
              <div class="info">
                <p class="name">{{item.name}}</p>
                <p class="detail">{{item.detail}}</p>
              </div>
            </div>
            <div class="col-2 t-center">
              <el-input-number class="stepper" size="mini" v-model="item.num" :min="1" :max="10" label="描述文字"></el-input-number>
            </div>
            <div class="col-2 t-center">
              <p class="price">{{item.price*item.num | handleCount}}</p>
            </div>
            <div class="col-1">
              <p @click="remove(item._id)" class="delete"><i class="iconfont icon-delete"></i>删除</p>
            </div>
          </div>
        </div>
        <div v-if="!cart.length" class="sorry">
          您的购物车是空的！
        </div>
      </div>
    </div>
    <div class="cart-bar">
      <div class="cart-box">
        <div class="bar-content">
          <div>
            <div @click="handleAll" class="checkbox">
              <i v-show="selectedAll" class="iconfont selected icon-selected"></i>
            </div>
            <span class="text">全选</span>
          </div>
          <div>
            <div class="text">商品合计：{{total | handleCount}}</div>
            <div class="buy">
              立即购买
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import KeyboardHttp from '../../models/KeyboardHttp.js'
export default {
  name:'Cart',
  data() {
    return {
      cart:'',
      selectedAll:''
    }
  },
  async mounted() {
    this.$bus.$emit('remove')

    var userName = JSON.parse(localStorage.getItem('login')).name
    document.title = `${userName}购物车`

    await KeyboardHttp.getCart(userName).then(res=>this.cart = res.data[0].cart)

    this.selectedAll = this.cart.every(item => item.isSelected)
  },
  filters:{
    handleCount(val){
      const options = {
        style: 'currency',
        currency: 'CNY',
      };
      return val.toLocaleString('zh-CN', options)
    }
  },
  computed:{
    total(){
      var total = 0
      var cart = JSON.parse(JSON.stringify(this.cart))
      for (var item of cart){
        if(item.isSelected){
          total += item.price*item.num
        }
      }
      return total
    },
  },
  methods:{
    handleSelect(index){
      this.cart[index].isSelected = !this.cart[index].isSelected
      this.selectedAll = this.cart.every(item => item.isSelected)
    },
    handleAll(){
      this.selectedAll = !this.selectedAll
      for(let item of this.cart){
        item.isSelected = this.selectedAll
      }
    },
    remove(id){
      var userName = JSON.parse(localStorage.getItem('login')).name
      if(confirm('确定删除吗')){
        this.$http.post(`/user/removecart`,{userName,id}).then(res=>{
          alert(res.data.msg)
        })
        this.cart = this.cart.filter(item => item._id != id)
      }
      
    }
  },
}
</script>

<style scoped>
  .cart{
    padding: 50px 0 180px 0;
    height: 100%;
    background-color: #fff;
  }
  .cart-box{
    width: 780px;
    margin: 0 auto;
  }
  .title{
    display: flex;
    align-items: flex-end;
    margin-top: 60px;
    margin-bottom: 15px;
  }
  .menu{
    font-size: 18px;
    color: #596481;
    cursor: pointer;
  }
  .gengduo{
    font-size: 22px;
    font-weight: 800;
    margin: 0 20px;
  }
  .type{
    font-size: 36px;
    color: #596481;
  }
  .title+hr{
    border: none;
    border-top: 1px solid #000;
    margin: 32px 0 46px;
  }
  .order{
    padding: 13px 28px;
  }
  .item-title{
    text-align: center;
    font-size: 14px;
    font-weight: 700;
    padding-bottom: 12px;
    border-bottom: 1px solid #ececec;
  }
   .item-title>div{
     font-size: 16px;
   }
  .t-center{
    text-align: center;
  }
  .item-container{
    margin-top: 30px;
    display: flex;
    align-items: center;
  }
  .img-cover{
    width: 101px;
    height: 100%;
    padding: 3px;
    border: 3px solid #ccc;
    background: #fff;
    text-align: center;
    float: left;
  }
  .img-cover>img{
    width: 75px;
    height: 50px;
  }
  .info{
    width: 260px;
    padding: 10px;
    float: left;
  }
  .info>p{
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .name{
    font-size: 16px;
    max-width: 100%;
    display: block;
    margin-right: 6px;
    color: #000;
    margin-bottom: 4px;
    line-height: 1.2em;
  }
  .detail{
    font-size: 12px;
    color: #3291ff;
    margin-top: 3px;
    line-height: 1em;
  }
  .stepper{
    width: 100px ;
  }
  .price{
    font-size: 14px;
    font-weight: 700;
  }
  .delete{
    background-color: rgb(230, 16, 16);
    padding: 5px;
    border-radius: 5px;
    color: #fff;
    cursor: pointer;
  }
  .delete:hover{
    background-color: rgb(180, 25, 25)
  }
  .checkbox{
    width: 18px;
    height: 18px;
    line-height: 18px;
    box-sizing: border-box;
    border: 3px solid #333;
    color: #333;
    background-color: #fff;
    position: relative;
    display: inline-block;
    vertical-align: middle;
    cursor: pointer;
  }
  .selected{
    font-weight: 800;
    font-size: 38px;
    position: absolute;
    top: -5px;
    left: -11px;
  }
  .cart-bar{
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ecedf1;
    font-size: 12px;
    color: #333;
    min-width: 1040px;
    z-index: 10;
  }
  .bar-content{
    padding: 8px 28px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .text{
    display: inline-block;
    font-size: 16px;
    margin:0 20px;
  }
  .buy{
    display: inline-block;
    padding: 0 28px;
    font-size: 18px;
    height: 44px;
    line-height: 44px;
    background-color: #0c0c0c;
    color: #fff;
    cursor: pointer;
  }
  .sorry{
     height: 200px;
    text-align: center;
    line-height: 200px;
    color: #596481;
    font-size: 40px;
  }
</style>